﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using IRUM.Core;
using IRUM.XingAPI;
using IRUM.Utils;
using System.Threading;

namespace IRUM.TR
{
    /// <summary>
    /// 해외선물 일차트
    /// 연결차트로는 조회되지 않음.
    /// 최근 30개만 조회됨.
    /// 이전 데이터는 수동으로 넣어둬야 함.
    /// 종목코드 규칙:
    ///     종목심볼(미니다우: YM) + 월(FGHJKMNQUVXZ) + 년(14)
    /// 저장할 때는 연결선물로 저장
    ///     종목심볼(미니다우: YN) + .1
    /// </summary>
    public class o3104 : CBaseQuery
    {
        public event ChartForeignDataHandler OnReceived;
        private List<ChartForeignData> m_list = new List<ChartForeignData>();

        private string shcode = string.Empty;
        private string lastDBDate = string.Empty;

        private string lastDataDate = string.Empty; //없는 데이터만 받기 위해 사용

        /// <summary>
        /// 해외지수 일/분 차트
        /// <param name="shcode">종목코드</param>
        /// <param name="lastDBDate">DB에 저장되어 있는 마지막 날짜</param>
        /// </summary>
        public o3104(string shcode, string lastDBDate)
        {
            this.shcode = shcode;
            this.lastDBDate = lastDBDate;

            SetClassData(TR_Type.o3104);
        }

        public void SetOutBlockData(int Index)
        {
            lastDataDate = m_Query.GetFieldData(m_OutBlock1Name, "date", Index);

            if (Convert.ToInt32(lastDataDate) > Convert.ToInt32(lastDBDate))
            {
                ChartForeignData cd = new ChartForeignData();
                cd.kind = "F";
                cd.shcode = shcode;
                cd.gubun = "D";
                cd.date = m_Query.GetFieldData(m_OutBlock1Name, "chedate", Index);
                cd.time = "";
                cd.open = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "open", Index));
                cd.high = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "high", Index));
                cd.low = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "low", Index));
                cd.close = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "price", Index));
                cd.volume = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "volume", Index));
                cd.kodate = "";
                cd.kotime = "";

                m_list.Add(cd);
            }
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlock1Name);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            // 이벤트 발생
            if (OnReceived != null)
            {
                OnReceived(this, new ChartForeignDataArgs(m_list));
            }
        }

        protected override void SetRequestData()
        {
            m_Query.SetFieldData(m_InBlockName, "gubun", 0, "0");                                   //조회구분, 8
            m_Query.SetFieldData(m_InBlockName, "shcode", 0, shcode);                               //종목코드, 8
            m_Query.SetFieldData(m_InBlockName, "date", 0, DateTime.Today.ToString("yyyyMMdd"));    //조회일자, 8
        }
    }
}
